package org.loong.crypto.service.software.provider.key;

import java.security.KeyPair;

import org.loong.crypto.common.exception.CryptoException;
import org.loong.crypto.common.exception.InvalidKeyLengthException;
import org.loong.crypto.core.algorithm.KeyPairAlgorithm;
import org.loong.crypto.service.core.provider.KeyPairGenerator;
import org.loong.crypto.service.core.provider.impl.BaseKeyPairGeneratorProvider;

import cn.hutool.crypto.KeyUtil;

/**
 * Generic KeyPair generator.
 */
public class GenericKeyPairGenerator extends BaseKeyPairGeneratorProvider implements KeyPairGenerator {

    @Override
    public KeyPair generateKeyPair(KeyPairAlgorithm algorithm, int keySize) throws CryptoException {
        if (algorithm.getKeyLength() > 0 && keySize < algorithm.getKeyLength()) {
            throw new InvalidKeyLengthException(algorithm.getKeyLength());
        }

        return KeyUtil.generateKeyPair(algorithm.getName(), keySize);
    }
}
